package com.cdks.wuxiu.api.controller;

import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.model.param.VideoDeleteParam;
import com.cdks.wuxiu.api.model.param.VideoGetInfoParam;
import com.cdks.wuxiu.api.model.param.VideoGetListParam;
import com.cdks.wuxiu.api.model.param.VideoPlayParam;
import com.cdks.wuxiu.api.model.param.VideoRewardParam;
import com.cdks.wuxiu.api.model.param.VideoUploadParam;
import com.cdks.wuxiu.api.service.MessageService;
import com.cdks.wuxiu.api.service.UserService;
import com.cdks.wuxiu.api.service.VideoService;
import com.cdks.wuxiu.api.util.SmsUtils;
import com.cdks.wuxiu.common.component.constant.ApiCodeEnum;
import com.cdks.wuxiu.common.model.api.ApiResult;
import com.cdks.wuxiu.entity.mysql.UserBaseInfo;
import com.cdks.wuxiu.entity.mysql.UserLoginInfo;
import com.cdks.wuxiu.entity.mysql.VideoShowInfo;

import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;


@Api(tags = "视频秀")
@Slf4j
@RestController
@RequestMapping("/video")
public class VideoController {
    @Resource
    private VideoService videoService;
    @Resource
    private UserService userService;
    @Resource
    private MessageService messageService;    


    @ApiOperation("查询用户视频信息")
    @PostMapping("/getInfo")
    public ApiResult<Object> getInfo(@RequestBody @Valid VideoGetInfoParam param) {
        return ApiResult.success(videoService.getVideoInfo(param.getVideoId()));
    }

    @ApiOperation("查询用户视频列表")
    @PostMapping("/getList")
    public ApiResult<Object> getList(@RequestBody @Valid VideoGetListParam param) {
        //未登录只能查看第一页
        if (param.getPage() > 0)
        {
            if ((param.getUserId() == null) || (param.getToken() == null))
            {
                return ApiResult.failed(ApiCodeEnum.NEED_LOGIN);
            }
        }

        List<VideoShowInfo> videos  = videoService.getUserVideos(param.getDestId(), param.getPage());

        return ApiResult.success(videos);
    }

    @ApiOperation("上传视频")
    @PostMapping("/upload")
    public ApiResult<Object> upload(@RequestBody @Valid VideoUploadParam param) {
        videoService.save(param.getUserId(), param.getVideoUri(), Constant.AUDIT_STATUS_WAIT);
        SmsUtils.sendManageNotice("用户视频待审");

        return ApiResult.success("提交成功，请等待审核");
    }

    @ApiOperation("删除视频")
    @PostMapping("/delete")
    public ApiResult<Object> delete(@RequestBody @Valid VideoDeleteParam param) {
        //删除数据
        videoService.delete(param.getUserId(), param.getVideoUri());

        return ApiResult.success();
    }

    @ApiOperation("播放视频")
    @PostMapping("/play")
    public ApiResult<Object> play(@RequestBody @Valid VideoPlayParam param) {
        String udid  = param.getUdid();
        UserBaseInfo userInfo  = param.getUserInfo();

        if (userInfo == null)
        {
            if (param.getCount() > 5)
            {
                JSONObject json  = new JSONObject();
                json.set("scheme", "xinsu://account/login");
                
                return ApiResult.failed(11, "查看更多精彩内容，请先登录", json);  
            }

            if (udid == null)
            {
                return ApiResult.success();
            }
        }
/*
        if (param.getUserId() == null)
        {
            if (param.getCount() > 3) {
                JSONObject json  = new JSONObject();
                json.set("scheme", "xinsu://account/login");
                
                return ApiResult.failed(11, "查看更多精彩内容，请先登录", json);  
            }
        }
        else
        {
            if (param.getCount() > 3) {
                JSONObject json  = new JSONObject();
                json.set("scheme", "xinsu://bean/recharge");
                
                return ApiResult.failed(11, "查看更多精彩内容，请先充值", json);  
            }            
        }
*/
        if ((udid == null) && (param.getUserId() != null)) {
            UserLoginInfo loginInfo  = userService.getUserLoginInfo(param.getUserId());
            udid  = loginInfo.getUdid();
        }

        //记录播放数据
        videoService.play(param.getUserId(), udid, param.getVideoId());

        return ApiResult.success();
    }

    @ApiOperation("打赏")
    @PostMapping("/reward")
    public ApiResult<Object> reward(@RequestBody @Valid VideoRewardParam param) {
        //扣费
        int ret  = videoService.charge(param.getVideoId(), param.getUserInfo(), param.getCost());
        if (ret < 0)
        {
            JSONObject json  = new JSONObject();
            json.set("scheme", "xinsu://bean/recharge");

            return ApiResult.failed(-10, "账户余额不足，请先充值", json);
        }

        return ApiResult.success();
    }
}
